<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>收到的认领 - 校园失物招领系统</title>
    <link rel="stylesheet" href="/css/common.css">
</head>
<body>
    <div class="container">
        <nav class="navbar">
            <div class="nav-left"><h1>校园失物招领系统</h1></div>
            <div class="nav-right">
                <span id="username"></span>
                <a href="/notifications" class="nav-link">通知 <span id="unreadCount" class="badge"></span></a>
                <a href="/profile" class="nav-link">个人中心</a>
                <a href="javascript:void(0)" onclick="logout()" class="nav-link">退出</a>
            </div>
        </nav>

        <div class="main-content">
            <div class="sidebar">
                <a href="/home" class="menu-item">首页</a>
                <a href="/lost" class="menu-item">失物信息</a>
                <a href="/found" class="menu-item">招领信息</a>
                <a href="/lost/publish" class="menu-item">发布失物</a>
                <a href="/found/publish" class="menu-item">发布招领</a>
                <a href="/my/lost" class="menu-item">我的失物</a>
                <a href="/my/found" class="menu-item">我的招领</a>
                <a href="/claims/sent" class="menu-item">发出的认领</a>
                <a href="/claims/received" class="menu-item active">收到的认领</a>
                <a href="/favorites" class="menu-item">我的收藏</a>
                <div id="adminMenu"></div>
            </div>

            <div class="content">
                <h2>收到的认领</h2>
                <div class="section">
                    <table>
                        <thead>
                            <tr>
                                <th>物品标题</th>
                                <th>认领人</th>
                                <th>验证信息</th>
                                <th>认领说明</th>
                                <th>申请时间</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody id="claimList"></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="/js/common.js"></script>
    <script>
        loadUser();
        loadUnreadCount();

        $.get('/api/claim/received', function(res) {
            if (res.code === 200) {
                if (res.data.length === 0) {
                    $('#claimList').html('<tr><td colspan="7" class="empty">暂无数据</td></tr>');
                } else {
                    const html = res.data.map(claim => `
                        <tr>
                            <td>${claim.itemTitle}</td>
                            <td>${claim.claimerName}</td>
                            <td>${claim.verifyInfo}</td>
                            <td>${claim.reason}</td>
                            <td>${formatDate(claim.createdAt)}</td>
                            <td><span class="status-badge status-${claim.status}">${getStatusText(claim.status)}</span></td>
                            <td>
                                ${claim.status === 0 ? `
                                    <button class="btn btn-success" onclick="reviewClaim(${claim.id}, 1)">通过</button>
                                    <button class="btn btn-danger" onclick="reviewClaim(${claim.id}, 2)">拒绝</button>
                                ` : claim.reply || ''}
                            </td>
                        </tr>
                    `).join('');
                    $('#claimList').html(html);
                }
            }
        });

        function getStatusText(status) {
            return ['待审核', '已通过', '已拒绝'][status];
        }

        function reviewClaim(id, status) {
            const action = status === 1 ? '通过' : '拒绝';
            const reply = prompt(`请输入${action}理由：`);
            if (reply !== null) {
                $.ajax({
                    url: `/api/claim/${id}/review`,
                    type: 'PUT',
                    data: { status: status, reply: reply },
                    success: function(res) {
                        if (res.code === 200) {
                            location.reload();
                        }
                    }
                });
            }
        }
    </script>
</body>
</html>

